extends Control

@onready var phone_anchor: Control = $PhoneAnchor
@onready var top_lid: ColorRect = $EyeOverlay/TopLid
@onready var bottom_lid: ColorRect = $EyeOverlay/BottomLid
@onready var sfx_vibrate: AudioStreamPlayer2D = $SFX_Vibrate
@onready var eye_overlay: Control = $EyeOverlay
@onready var bedroom_bg: TextureRect = $BedroomBackground
@onready var sfx_send: AudioStreamPlayer2D = $SFX_Send
@onready var sfx_receive: AudioStreamPlayer2D = $SFX_Receive

var phone_off_screen_pos: Vector2
var phone_final_pos: Vector2

@onready var button_1: Button = $Button1
@onready var button_2: Button = $Button2
@onready var button_3: Button = $Button3


const IntroScene = preload("res://scene/IntroSequence.tscn")


func _ready() -> void:
	# --- 初始化设置 ---
	VN_SystemUI.hide_hud() #把HUD隐藏
	VN_SystemUI.force_hide_drink_menu()
	# 记录手机位置并移出屏幕
	phone_final_pos = phone_anchor.position
	phone_off_screen_pos = phone_final_pos + Vector2(0, 1000)
	phone_anchor.position = phone_off_screen_pos
	
	# 强制闭眼
	var screen_size = get_viewport_rect().size
	top_lid.size = Vector2(screen_size.x, screen_size.y / 2)
	top_lid.position = Vector2(0, 0)
	bottom_lid.size = Vector2(screen_size.x, screen_size.y / 2)
	bottom_lid.position = Vector2(0, screen_size.y / 2)
	eye_overlay.visible = true
	
	#实例化字幕开场
	var intro = IntroScene.instantiate()
	add_child(intro) #加到最上层
	#监听字幕结束信号
	intro.intro_finished.connect(_on_intro_finished)
	

func start_waking_sequence() -> void:
	#震动循环
	#循环 2 次。
	for i in range(2):
		#播放声音
		if sfx_vibrate.stream:
			#为了防止声音重叠，每次播放前强制停止上一次
			sfx_vibrate.stop() 
			sfx_vibrate.play()
		
		#背景抖动特效(每次震动都抖一下)
		var shake_tween = create_tween()
		#快速随机晃动5次
		for j in range(5):
			shake_tween.tween_property(bedroom_bg, "position", Vector2(randf_range(-5, 5), randf_range(-5, 5)), 0.05)
		#最后归位
		shake_tween.tween_property(bedroom_bg, "position", Vector2(0,0), 0.05)
		
		#等待间隔
		await get_tree().create_timer(1.5).timeout

	#睁眼
	print("震动结束，开始睁眼...")
	
	var eye_tween = create_tween()
	eye_tween.set_parallel(true)
	eye_tween.set_ease(Tween.EASE_IN_OUT).set_trans(Tween.TRANS_CUBIC)
	
	#上眼皮上移，下眼皮下移
	eye_tween.tween_property(top_lid, "position:y", -top_lid.size.y, 2.5)
	eye_tween.tween_property(bottom_lid, "position:y", get_viewport_rect().size.y, 2.5)
	
	#眼皮淡出透明
	eye_tween.tween_property(top_lid, "modulate:a", 0.0, 2.0)
	eye_tween.tween_property(bottom_lid, "modulate:a", 0.0, 2.0)
	
	await eye_tween.finished
	eye_overlay.visible = false 
	
	#手机滑入
	var phone_tween = create_tween()
	phone_tween.set_ease(Tween.EASE_OUT).set_trans(Tween.TRANS_CUBIC)
	
	phone_tween.tween_property(phone_anchor, "position", phone_final_pos, 2.0)
	
	await phone_tween.finished
	
	#开始正式逻辑
	_start_dialogue_system()

func _start_dialogue_system() -> void:
	print("流程结束，正式开始游戏交互")
	$PhoneAnchor/Background/View.get_v_scroll_bar().changed.connect(_Refresh_View)
	button_1.pressed.connect(_Pressed_question1)
	button_2.pressed.connect(_Pressed_question2)
	button_3.pressed.connect(_Pressed_question3)
	await Add_text("mm亲！今天晚上一起吃饭嘛｡:.ﾟヽ(*´∀`)ﾉﾟ.:｡", 1)
	await Add_text("吃了一起去店里呗", 1)
	button_1.visible = true

func _on_intro_finished():
	#字幕播完了，此时屏幕依然是黑的
	print("字幕结束，准备震动...")
	start_waking_sequence()
	
	
	
func _Refresh_View():
	$PhoneAnchor/Background/View.set_deferred("scroll_vertical",$PhoneAnchor/Background/View.get_v_scroll_bar().max_value)
	
func _Pressed_question1():
	button_1.visible = false
	await Add_text("好呀", 0)
	button_2.visible = true
	
func _Pressed_question2():
	button_2.visible = false
	await Add_text("等我稍微收拾一下就出门", 0)
	button_3.visible = true
	
func _Pressed_question3():
	button_3.visible = false
	await Add_text("我们在车站见吧", 0)
	await Add_text("okk", 1)
	SceneLoader.change_scene_with_grid_spread("res://scene/start_.tscn")
	
func Add_text(content, source):
	var text_buttle = load("res://scene/text_buttle.tscn").instantiate()
	text_buttle.text_content = content
	text_buttle.text_source = source
	$PhoneAnchor/Background/View/Container.add_child(text_buttle)
	
	if source == 0:
		#如果是主角(我)发的，播放发送音效
		if sfx_send.stream: 
			sfx_send.play()
	else:
		#如果是对方发的，播放接收音效
		if sfx_receive.stream:
			sfx_receive.play()
	
	if source == 0:
		await get_tree().create_timer(1.0).timeout
	else:
		await get_tree().create_timer(1.5).timeout
